<?php
/**********************************CLASS************************************ *
* Class Name: HTMLMgrDef
* Author: Nicholas Malacarne
* Description: Useful for building HTML pages with PHP.
***************************************************************************** */
class HTMLMgrDef
{
    private $author,
            $title,
            $thePage,
            $css,
            $entities;
    
/***************************************************************************** *
**********************************CONSTRUCTOR!******************************** *
* Author: Nicholas Malacarne
* Parameters: $pageAuthor - The author of the page.
 *            $pTitle - The title for the page.
* Description: Initializes a HTMLMgrDef obj.
***************************************************************************** */
    function __construct($pageAuthor = "", $pTitle = "")
    {
        $this->entities = array();
        $this->setAuthor($pageAuthor);
        $this->setTitle($pTitle);
        $this->glue("<!--\n");
        $this->glue("# Created using cf.Studios' HTMLMgrDef.php\n");
        $this->glue("# Version: 0.1\n");
        $this->glue("# Page Name: $pTitle\n");
        $this->glue("# Page Author: $pageAuthor\n");
        $this->glue("-->\n");
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: createEntity
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Creates an element in an array to hold HTML code. This allows
 * the user to build specific parts of a page without adding them directly 
 * into the page when they are created.
***************************************************************************** */
    function createEntity($name)
    {
        $this->entities[$name] = "";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: glue
* Author: Nicholas Malacarne
* Parameters: $content - What to place on the page.
* Description: Places the content on the page.
***************************************************************************** */
    function glue($content)
    {
            $this->thePage .= $content; 
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: getInButton
* Author: Nicholas Malacarne
* Parameters: $type - The type of input box.
 *            $value - The name of the input box.
* Description: Return the HTML code for an input button.
***************************************************************************** */
    function getInButton($type, $value)
    {
        return "<input type=\"$type\" value=\"$value\"";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: getInBox
* Author: Nicholas Malacarne
* Parameters: $type - The type of input box.
 *            $name - The name of the input box.
 *            $value - The value to store in the input box.
* Description: Return the HTML code for an input box.
***************************************************************************** */
    function getInBox($type, $name, $value = null)
    {
        if (!$value == null || $value == 0)
        {
            return "<input type=\"$type\" name=\"$name\" value=\"$value\" />";
        }
        else
        {
            return "<input type=\"$type\" name=\"$name\" />";
        }
    }

/**********************************FUNCTION*********************************** *
* Function Name: getImg
* Author: Nicholas Malacarne
* Parameters: $URL - The URL of the image.
* Description: Returns the image HTML.
***************************************************************************** */
    function getImg($URL)
    {   
        return "<img src=\"".$URL."\">";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: getInImg
* Author: Nicholas Malacarne
* Parameters: $URL - The URL of the image.
* Description: Adds an input box to the page.
***************************************************************************** */
    function getInImg($URL)
    {
        return "<input type=\"image\" src = \"$URL\">";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: addTData
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity. 
 *            $content - The data we need to store.
* Description: Stores content in a table data spot.
***************************************************************************** */
    function addTData($name, $content)
    {
        $this->entities[$name] .= $this->getTag("td", $content)."\n";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: startTRow
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Starts a row in a table.
***************************************************************************** */
    function startTRow($name)
    {
        $this->entities[$name] .= "<tr>\n";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: startTable
* Author: Nicholas Malacarne
* Parameters: $tableName - The name of the table.
 *            $border - The border size of the table. (Default: 0)
* Description: Starts an HTML table.
***************************************************************************** */
    function startTable($name, $border=0)
    {
        $this->entities[$name] = "<table border=\"$border\">\n";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: addTextArea
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
 *            $cols - The column span of the textarea.
 *            $rows - The row span of the textarea.
 *            $content - The content you wish to tag.
 *            $readOnly - Whether the textarea is readonly.
* Description: Enwraps content in a textarea.
***************************************************************************** */
    function addTextArea($name, $cols, $rows, $content, $readOnly = false)
    {
        if ($readOnly)
        {
            $this->entities[$name] .= "<textarea rows=\"$rows\" cols=\"$cols\" ".
                                      "readonly=\"readonly\">\n".$content."</textarea>";
        }
        else
        {
            $this->entities[$name] .= "<textarea rows=\"$rows\" cols=\"$cols\"
                                      >$content</textarea>";
        }
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: addInBox
* Author: Nicholas Malacarne
* Parameters: $name - The name of the input box.
 *            $type - The type of input box.
 *            $txtName - The name to give the textbox.
* Description: Adds an input box to the page.
***************************************************************************** */
    function addInBox($name, $type, $txtName, $value = null)
    {
        $this->addHTML($name, $this->getInBox($type, $txtName, $value));
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: addInImg
* Author: Nicholas Malacarne
* Parameters: $name - The name of the input box.
 *            $URL - The URL of the image.
* Description: Adds an input box to the page.
***************************************************************************** */
    function addInImg($name, $URL)
    {
        $this->addHTML($name, $this->getInImg($URL));
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: addImg
* Author: Nicholas Malacarne
* Parameters: $name -The name of the entity.
 *            $URL - The URL of the image.
* Description: Adds an image to the page.
***************************************************************************** */
    function addImg($name, $URL)
    {
        $this->addHTML($name, $this->getImg($URL));
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: endTable
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Ends a table.
***************************************************************************** */
    function endTable($name)
    {
        $this->entities[$name] .= "</table>";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: endTRow
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Ends a row for the table.
***************************************************************************** */
    function endTRow($name)
    {
        $this->entities[$name] .= "</tr>\n";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: endForm
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Ends an HTML form.
***************************************************************************** */
    function endForm($name)
    {
        $this->entities[$name] .= "</form>";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: endDiv
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Ends a division element in HTML.
***************************************************************************** */
    function endDiv($name)
    {
        $this->addHTML($name, "</div>");
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: startDiv
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
 *            $id - The ID to give the DIV.
* Description: Starts a division element in HTML.
***************************************************************************** */
    function startDiv($name, $id)
    {
        $this->addHTML($name, "<div id=\"".$id."\">");
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: getEntity
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Returns the HTML entity.
***************************************************************************** */
    function getEntity($name)
    {
        return $this->entities[$name];
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: getPage
* Author: Nicholas Malacarne
* Parameters: NONE
* Description: Returns the page.
***************************************************************************** */
    function getPage()
    {
        return $this->thePage;
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: startForm
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
 *            $formName - The name to give the form.
 *            $action - The action the form should take.
 *            $method - The method for the form to use. (Default: post)
* Description: Starts a form and adds it to an HTML entity.
***************************************************************************** */
    function startForm($name, $formName, $action, $method="post")
    {
        $this->entities[$name] .= "<form name=\"$formName\" ";
        $this->entities[$name] .= "action=\"$action\" ";
        $this->entities[$name] .= "method=\"$method\">\n";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: addHTML
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
 *            $content - The HTML to add to the page.
* Description: Adds the given HTML to the page.
***************************************************************************** */
    function addHTML($name, $content)
    {
        $this->entities[$name] .= $content;
        $this->entities[$name] .="\n";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: setAuthor
* Author: Nicholas Malacarne
* Parameters: $pageAuthor - The author of the page.
* Description: Sets the author of the page.
***************************************************************************** */
    function setAuthor($pageAuthor)
    {
        $this->author = $pageAuthor;
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: getTitle
* Author: Nicholas Malacarne
* Parameters: NONE
* Description: Returns the title of the page.
***************************************************************************** */
    function getTitle()
    {
        return $this->title;
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: setTitle
* Author: Nicholas Malacarne
* Parameters: $pTitle - Title of the page.
* Description: Sets the name of the page.
***************************************************************************** */
    function setTitle($pTitle)
    {
        $this->title = $pTitle;
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: resetEntity
* Author: Nicholas Malacarne
* Parameters: $name - The entity name.
* Description: Resets the entity to a blank string.
***************************************************************************** */
    function resetEntity($name)
    {
        $this->entities[$name] = "";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: setCSS
* Author: Nicholas Malacarne
* Parameters: $URL - The URL to the CSS template.
* Description: Sets the CSS template that the page will utilize.
***************************************************************************** */
    function setCSS($URL)
    {
        $this->css = "<link rel=\"stylesheet\" "; 
        $this->css .= "type=\"text/css\" ";
        $this->css .= "href=\"".$URL."\" />";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: getTag
* Author: Nicholas Malacarne
* Parameters: $tag - The tag you wish to apply.
 *            $content - the content you wish to tag.
* Description: Enwraps text in HTML tags and returns it.
***************************************************************************** */
    function getTag($tag, $content)
    {
        return "<$tag>\n$content\n</$tag>";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: addTHeader
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
 *            $content - The content to place within the header.
* Description: Adds a header to a table.
***************************************************************************** */
    function addTHeader($name, $content, $colspan=1)
    {
        $this->entities[$name] .= "<th colspan=\"$colspan\">$content</th>\n";
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: addTag
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
 *            $tag - The tag you wish to apply.
 *            $content - the content you wish to tag.
* Description: Enwraps text in HTML tags.
***************************************************************************** */
    function addTag($name, $tag, $content)
    {
        $this->addHTML($name, $this->getTag($tag, $content));
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: startHTMLHead
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Builds the top of the HTML page.
***************************************************************************** */
    function startHTMLHead($name="HEAD")
    {
        $this->addHTML($name, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        $this->addHTML($name, "<head>");
        $this->addTag($name, "title", $this->getTitle());
        
        if(is_string($this->css))
        {
            $this->addHTML($name, $this->css);
        }
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: endHTMLHead
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Ends the head section of the HTML.
***************************************************************************** */
    function endHTMLHead($name="HEAD")
    {
        $this->addHTML($name, "</head>");
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: startHTMLBody
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Starts the body section of the HTML.
***************************************************************************** */
    function startHTMLBody($name="BODY")
    {
        $this->addHTML($name, "<body>");
    }
    
/**********************************FUNCTION*********************************** *
* Function Name: endHTMLBody
* Author: Nicholas Malacarne
* Parameters: $name - The name of the entity.
* Description: Ends the body section of the HTML.
***************************************************************************** */
    function endHTMLBody($name="BODY")
    {
        $this->addHTML($name, "</body>");
    }
}

?>
